package com.bjtu.literatureclient.service.user.impl;

import com.bjtu.literatureclient.dao.team.ITeamDao;
import com.bjtu.literatureclient.dao.user.IUserDao;
import com.bjtu.literatureclient.domain.base.BaseModel;
import com.bjtu.literatureclient.domain.team.Team;
import com.bjtu.literatureclient.domain.user.User;
import com.bjtu.literatureclient.domain.user.UserAndTeam;
import com.bjtu.literatureclient.service.user.IUserService;
import com.bjtu.literatureclient.shiro.UserRealm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements IUserService {

	private final static String[] teamMembers = {"3", "4", "5", "6"};
	private final static String[] teamAdmins = {"0", "1", "2"};
    @Autowired
    private IUserDao userDao;

	@Autowired
	private ITeamDao teamDao;

    @Override
    public List<User> query(BaseModel baseModel) {
    	
    	
        return this.userDao.query(baseModel);
    }

    @Override
    public int update(User user) {
        return this.userDao.update(user);
    }

    @Override
    public int insert(User user) {
        return userDao.insert(user);
    }

	@Override
	public int delete(BaseModel baseModel) {
		// TODO Auto-generated method stub
		return this.userDao.delete(baseModel);
	}

	@Override
	public User login(User user) {
		User userNew=userDao.login(user);
		return userNew;
	}

	@Override
	public User loginByPhone(User user) {
		User userNew=userDao.loginByPhone(user);
		return userNew;
	}

	@Override
	public User loginByEmail(User user) {
		User userNew=userDao.loginByEmail(user);
		return userNew;
	}

	@Override
	public String checkUserEmail(String userEmail) {
    	List<User> user = userDao.selectUserByEmail(userEmail);
		if (user.size() == 0) {
			return "y";
		}
		return "n";
	}

	@Override
	public boolean checkUserNo(String userNo) {
    	List<User> list= userDao.checkUserNo(userNo);
    	if (list.size() > 0) {
			System.out.println("serverice 层测试："+list.get(0));
			return true;
		}
		return false;
	}

	@Override
	public int checkUserState(String userNo) {
		return userDao.checkUserState(userNo);
	}

	@Override
	public int checkUserStateByPhone(String userNo) {
		return userDao.checkUserStateByPhone(userNo);
	}

	@Override
	public int checkUserStateByEmail(String userNo) {
		return userDao.checkUserStateByEmail(userNo);
	}

	@Override
	public String getNextUserNo(String teamNo) {
		String newUserNo = userDao.getNextUserNo(teamNo);
		return newUserNo;
	}

	@Override
	public boolean setUserState(String userNo, String userState) {
    	boolean result = userDao.setUserState(userNo, userState);
		return result;
	}

	@Override
	public List<UserAndTeam> getUsersByTeamNo(String teamNo) {
		List<User> users=new ArrayList<User>();
		users = userDao.getUsersByTeamNo(teamNo);
		List<UserAndTeam> userteams =new ArrayList<UserAndTeam>();
		for (User user : users) {
			UserAndTeam userteam =new UserAndTeam();

            int roleIndex = 6;
            try {
                roleIndex = Integer.parseInt(user.getUserRole());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
			String roleName= UserRealm.roleNameList[roleIndex];

			userteam.setUserRoleName(roleName);
			userteam.setUserPhone(user.getUserPhone());
			userteam.setUserEmail(user.getUserEmail());
			userteam.setUserName(user.getUserName());
			userteam.setUserNo(user.getUserNo());
			userteams.add(userteam);
		}
		return userteams;
	}

	@Override
	public String loginRedirect(User user) {
		String fallbackUrl = "/index.html";
		String role = user.getUserRole();

		if (user.getUserState().equals("1")) {
			// 若为第一次登陆
			if (Arrays.asList(teamAdmins).contains(role)) {
				// 若为管理员
				fallbackUrl = "/admin/memberAdd.html";
			}else if (Arrays.asList(teamMembers).contains(role)) {
				// 若为团员
				fallbackUrl = "/miuser/indeAdmin.html";
			}
		}else if(user.getUserState().equals("2")){
			if (Arrays.asList(teamAdmins).contains(role)) {
				// 若为管理员
				fallbackUrl = "/admin/admin.html";
			}else if (Arrays.asList(teamMembers).contains(role)) {
				// 若为团员
				fallbackUrl = "/miuser/playground.html";
			}
		}
		return fallbackUrl;
	}

	@Override
	public String getNewUserNo(String teamNo) {
		String userNo = userDao.selectUserMaxIdByTeamId(teamNo);
        Team team = teamDao.selectTeamById(teamNo);
		int maxId = 0;
		if (userNo != null) {
			maxId = Integer.parseInt(userNo.substring(1));
		}
		// 若超过限制，返回-1
		if (team.getCurrentNum() >= team.getMaxNum()) {
            return "-1";
		}
		// 成功返回下一个用户号
		return "U" + String.format("%08d", maxId+1);
	}
}
